package java8;

import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class GroupByTest1 {

    void fun(int pos) {
        final int tmp = pos;
        List<List<Object>> disList = Arrays.asList(Arrays.asList("ZJ", "HZ"), Arrays.asList("ZJ", "SX"), Arrays.asList("JS", 123), Arrays.asList("JS", null), Arrays.asList("AH", "HS"));


        Map<PrePart, PostPart> map = disList.stream().collect(
                Collectors.groupingBy((List<Object> list) -> {
                            PrePart p = new PrePart();
                            p.setData(list.subList(0, tmp));
                            return p;
                        }, LinkedHashMap::new, Collectors.mapping((List<Object> list) -> list.subList(tmp, list.size()), Collector.of(PostPart::new, PostPart::add, (PostPart left, PostPart right) -> {
                            left.getData().addAll(right.getData());
                            return left;
                        },
                        Collector.Characteristics.IDENTITY_FINISH))
                ));
        System.out.println(map);
    }

    @Data
    private class PrePart {
        private List<Object> data = new ArrayList<>();

        public boolean isEmpty() {
            return CollectionUtils.isEmpty(data);
        }
    }

    @Data
    private class PostPart {
        private List<List<Object>> data = new ArrayList<>();

        public void add(List<Object> one) {
            data.add(one);
        }

        public boolean isEmpty() {
            return CollectionUtils.isEmpty(data);
        }
    }

    public static void main(String[] args) {
        new GroupByTest1().fun(2);
    }


}
